/**
 * lhj
 */

import chalk from 'chalk'
import { program } from 'commander'
import MCryptoTs from './MCryptoTs'
import Mrsa from './MRas'
import KillChain from './KillChain'

program
    .version(require('../package.json').version)
    .description('常用工具集')
    .usage('<command> [option]')
// base64
program
    .command('base64 <data>')
    .description('base64 加/解密')
    .option('-d, --decode', '解密', false)
    .action((data, options)=>{
        //
        const decode = options['decode']
        if (decode) {
            const res = Buffer.from(data, 'base64').toString()
            console.log(res)
        } else {
            const res = Buffer.from(data).toString('base64')
            console.log(res)
        }
    })

// hash
program
    .command('hash <data>')
    .description('hash 加密')
    .option('-m, --method [options]', 'hash 加密方式', 'md5')
    .action((data, options)=>{
        const method = options['method']
        let res = null
        switch (method) {
            case 'sha1':
                res = MCryptoTs.sha1(data)
                break
            case 'sha256':
                res = MCryptoTs.sha256(data)
                break
            case 'sha512':
                res = MCryptoTs.sha512(data)
                break
            default:
                res = MCryptoTs.md5(data)  
        }
        console.log(res)
    })
// rsa
program
    .command('rsa <data>')
    .description('rsa 非对称加密')
    .option('-k, --key', '生成密钥对', false)
    .option('-kp, --keyPath <keyPath>', '密钥对输出目录路径')
    .option('-ksp, --keysPath <keys_path>', '密钥对。keys_path: 密钥对文件路径')
    .option('-pri, --private <priKey_path>', '私钥。priKey_path: 私钥文件路径')
    .option('-pub, --public <pubKey_path>', '公钥。pubKey_path: 公钥文件路径')
    .option('-e, --encrypt', '加密', true)
    .option('-d, --decrypt', '解密', false)
    .option('-sign, --signature', '进行私钥签名',  false)
    .option('-verify, --verify <signature>', '对签名进行验证。signature: 签名')
    .action((data, options)=>{
        const key = options['key']
        const keyPath = options['keyPath']
        const keysPath = options['keysPath']
        const priKeyPath = options['private']
        const pubKeyPath = options['public']
        const encrypt = options['encrypt']
        const decrypt = options['decrypt']
        const signature = options['signature']
        const verify = options['verify']
        if (key) {
            Mrsa.getKeys(keyPath)
            process.exit(0)
        }
        if (!keysPath && !priKeyPath && !pubKeyPath) {
            console.log(chalk.red('请至少指定一个参数 -ksp -pri -pub'))
            process.exit(-1)
        }
        // 私钥签名
        if (signature) {
            if (!priKeyPath && !keysPath) {
                console.log(chalk.red('请指定 --pri 或 -ksp 参数'))
                process.exit(-1)
            }
            Mrsa.signature(data, priKeyPath, keysPath)
            process.exit(0)
        }
        if (verify) {
            if (!pubKeyPath && !keysPath) {
                console.log(chalk.red('请指定 --pub 或 -ksp 参数'))
                process.exit(-1)
            }
            Mrsa.verify(data, verify, pubKeyPath, keysPath)
            process.exit(0)
        }
        // 私钥解密
        if (decrypt) {
            // console.log('解密')
            if (!priKeyPath && !keysPath) {
                console.log(chalk.red('请指定 --pri 或 -ksp 参数'))
                process.exit(-1)
            }
            Mrsa.decryptByPriKey(data, priKeyPath, keysPath)
            process.exit(0)
        }
        // 公钥加密 [默认]
        if (encrypt) {
            // console.log('加密')
            if (!pubKeyPath && !keysPath) {
                console.log(chalk.red('请指定 --pub 或 -ksp 参数'))
                process.exit(-1)
            }
            Mrsa.encryptByPubKey(data, pubKeyPath, keysPath)
            process.exit(0)
        }
        if (signature && !verify) {

        }
    })
// killchain
program
    .command('killchain')
    .description('杀链')
    .action((options)=>{
        const killChain = new KillChain()
        killChain.start()
    })


program.parse(process.argv)
